#include "../logs/log.h"
#include <vector>
#include <chrono>

//测试性能接口
void bench(const std::string &logger_name, size_t thread_cnt, size_t msg_count,size_t msg_len){
    //1.获取日志器
    mylog::logger::ptr logger = mylog::getLogger(logger_name);
    //假如获取失败，不存在该日志器，则直接返回
    if(logger.get() == nullptr){
        return;
    }
    std::cout << "测试日志：" << msg_count << " 条， 总大小: " << (msg_count * msg_len) / 1024 << "KB" << std::endl;
    //2.组织指定长度的日志器
    std::string msg(msg_len - 1, 'A');  //保留最后一个位置，放置换行符
    //3.创建指定数量的线程
    std::vector<std::thread> threads;
    std::vector<double> cost_arry(thread_cnt);
    size_t msg_per_thr = msg_count/thread_cnt;  //每个线程平均输出的日志条数
    //4.线程函数内部开始计时
    for (int i = 0; i < thread_cnt;i++){
        threads.emplace_back([&, i](){
                //5.开始循环写日志
                auto start = std::chrono::high_resolution_clock::now();
                //开始循环写日志
                for(int j = 0; j < msg_per_thr; j++){
                     logger->fatal("%s", msg.c_str());
                }
                //6.线程函数内部结束计时
                auto end = std::chrono::high_resolution_clock::now();
                std::chrono::duration<double> cost = end -start;
                cost_arry[i] = cost.count();
                std::cout << "线程" << i << ": " << "\t线程数量:" << msg_per_thr << ",耗时:" << cost.count() << "s\n"; 
        });
    }
    for (int i = 0; i < thread_cnt;i++){
        threads[i].join();
    }
    //7.计算总耗时
    double max_cost = cost_arry[0];
    for (int i = 0;i < thread_cnt;i++) max_cost = (max_cost < cost_arry[i])?cost_arry[i]:max_cost;
    size_t msg_per_sec = msg_count / max_cost;
    size_t size_per_sec = (msg_count * msg_len) / (max_cost * 1024);
    //8.进行输出打印
    std::cout << "\t总耗时:" << max_cost << "s\n";
    std::cout << "\t每秒输出日志数量:" << msg_per_sec << " 条\n";
    std::cout << "\t每秒输出日志大小:" << size_per_sec << "KB\n";
}

void sync_bench(){
    std::unique_ptr<mylog::LoggerBuilder> builder(new mylog::GlobalLoggerBuilder());
    builder->BuildLoggerName("sync_logger");
    builder->BuildLoggerType(mylog::LoggerType::Sync_logger);  //采用同步日志器
    builder->BuildFormatter("[%c][%f:%l][%p]%T%m%n");
    builder->BuildLoggerLevel(mylog::LogLevel::value::DEBUG);
    builder->BuildSink<mylog::FileSink>("./logfile/sync.log");
    builder->build();
    bench("sync_logger",1,2000000, 100); //一个线程，1000000条100字节日志
    //bench("sync_logger",1,1000000, 100); //三个线程，1000000条100字节日志
}

void async_bench(){
    std::unique_ptr<mylog::LoggerBuilder> builder(new mylog::GlobalLoggerBuilder());
    builder->BuildLoggerName("async_logger");
    builder->BuildLoggerType(mylog::LoggerType::Async_logger);  //采用异步日志器
    builder->BuildLooperNotSafe(); //开启非安全模式
    builder->BuildFormatter("[%c][%f:%l][%p]%T%m%n");
    builder->BuildLoggerLevel(mylog::LogLevel::value::DEBUG);
    builder->BuildSink<mylog::FileSink>("./logfile/async.log");
    builder->build();
    bench("async_logger",1,2000000, 100); //一个线程，1000000条100字节日志
    //bench("async_logger",1,1000000, 100); //三个线程，1000000条100字节日志
}

int main(){
    sync_bench();
    async_bench();
    return 0;
}